package com.adms.service;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.adms.domain.Image;
import com.adms.domain.ImageMapper;
import com.adms.util.FileUtil;

@Transactional
@Service
public class ImageService {

	@Autowired
	private ImageMapper imageMapper;

	public Integer uploadImg(MultipartFile file) {
		String filePath = FileUtil.uploadImg(file);
		String url = FileUtil.getUrlByFilePath(filePath);
		Image image = new Image();
		image.setUrl(url);
		imageMapper.insertSelective(image);
		return image.getImageId();
	}

	public List<Integer> uploadImg(MultipartFile[] files) {
		List<Integer> imageIds = new ArrayList<>();
		String filePath;
		String url;
		Image image;
		for (MultipartFile file : files) {
			filePath = FileUtil.uploadImg(file);
			url = FileUtil.getUrlByFilePath(filePath);
			image = new Image();
			image.setUrl(url);
			imageMapper.insertSelective(image);
			imageIds.add(image.getImageId());
		}
		return imageIds;
	}

	public void getCutImage(Integer imageId, Integer width, Integer height, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		Image image = imageMapper.selectByPrimaryKey(imageId);
		if (image == null) {
			throw new RuntimeException("没有指定图片");
		}
		String url = image.getUrl();
		String filePath = FileUtil.getFilePathByUrl(url);
		File imageFile = new File(filePath);
		if (!imageFile.exists()) {
			throw new RuntimeException("图片路径错误");
		}
		response.setContentType("image/png");
		OutputStream stream = response.getOutputStream();
		byte[] data = FileUtil.cutImage(imageFile, width, height).toByteArray();
		stream.write(data);
		stream.flush();
		stream.close();
	}

	public void getImg(Integer imageId, Integer width, Integer height, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		Image image = imageMapper.selectByPrimaryKey(imageId);
		if (image == null) {
			throw new RuntimeException("没有指定图片");
		}
		String url = image.getUrl();
		String filePath = FileUtil.getFilePathByUrl(url);
		File imageFile = new File(filePath);
		if (!imageFile.exists()) {
			throw new RuntimeException("图片路径错误");
		}
		response.setContentType("image/png");
		OutputStream stream = response.getOutputStream();
		byte[] data = FileUtil.thumbnailImage(imageFile, width, height).toByteArray();
		stream.write(data);
		stream.flush();
		stream.close();
	}

	public boolean delete(int[] imageIds) {
		if (imageIds != null) {
			int length = imageIds.length;
			if (length > 0) {
				if (length == 1) {
					String url = imageMapper.selectUrlAndDeleteByPrimaryKey(imageIds[0]);
					FileUtil.deleteUrl(url);
				} else if (length > 1) {
					List<String> urlList = imageMapper.selectUrlAndDeleteBatchAnd(imageIds);
					FileUtil.deleteUrls(urlList);
				}
			}
		}
		return true;
	}

}
